package com.example.teesystem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teesystem.common.component.AutoUpdate;
import com.example.teesystem.common.exception.CustomException;
import com.example.teesystem.common.utils.IssueUtil;
import com.example.teesystem.common.utils.Response;
import com.example.teesystem.common.utils.ResultCode;
import com.example.teesystem.entity.Issue;
import com.example.teesystem.entity.IssueRecord;
import com.example.teesystem.entity.vo.IssueRecord.IssueRecordInsertVo;
import com.example.teesystem.filter.MyHandlerInterceptor;
import com.example.teesystem.mapper.IssueMapper;
import com.example.teesystem.mapper.IssueRecordMapper;
import com.example.teesystem.service.IssueRecordService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 12510
 * @description 针对表【t_issue_record】的数据库操作Service实现
 * @createDate 2022-04-14 19:38:55
 */
@Service
@AllArgsConstructor
public class IssueRecordServiceImpl extends ServiceImpl<IssueRecordMapper, IssueRecord>
        implements IssueRecordService {

    private final IssueRecordMapper issueRecordMapper;
    private final IssueMapper issueMapper;

    private static int ERROR = -1;

    @Override
    public Response addIssueRecordService(IssueRecordInsertVo issueRecordInsertVo) {
        int id = ERROR;
        try {
            id = issueRecordMapper.getIdByMsg(issueRecordInsertVo.getIssueId(), MyHandlerInterceptor.getUid());
        } catch (Exception e) {
            id = ERROR;
        }

        Issue issue = issueMapper.getIssueMessageById(issueRecordInsertVo.getIssueId());
        Boolean right = checkAnswer(issueRecordInsertVo.getIssueId(), issueRecordInsertVo.getAnswer());
        int iId = ERROR;
        if (id == ERROR) {
            IssueRecord issueRecord = new IssueRecord();
            issueRecord.setIssueId(issueRecordInsertVo.getIssueId());
            issueRecord.setAnswer(issueRecordInsertVo.getAnswer());
            issueRecord.setStudentId(MyHandlerInterceptor.getUid());
            issueRecord.setCourseId(issue.getCourseId());
            if (right) {
                issueRecord.setIsRight(1);
                issueRecord.setRightNum(1);
                issueRecordMapper.insert(issueRecord);
                iId = issueRecord.getId();
                if (iId == ERROR) {
                    return Response.error(ResultCode.INSERT_ERROR.getMsg(), ResultCode.INSERT_ERROR.getCode());
                } else {
                    return Response.ok();
                }
            } else {
                issueRecord.setIsRight(0);
                issueRecord.setWrongNum(1);
                issueRecordMapper.insert(issueRecord);
                iId = issueRecord.getId();
                if (iId == ERROR) {
                    return Response.error(ResultCode.INSERT_ERROR.getMsg(), ResultCode.INSERT_ERROR.getCode());
                } else {
                    return Response.ok();
                }
            }
        } else {
            if (right) {
                issueRecordMapper.addRightNum(id, MyHandlerInterceptor.getUid(), issueRecordInsertVo.getAnswer());
                return Response.ok();
            } else {
                issueRecordMapper.addWrongNum(id, MyHandlerInterceptor.getUid(), issueRecordInsertVo.getAnswer());
                return Response.ok();
            }
        }

    }

    @Override
    public Boolean checkAnswer(int issueId, String answer) {
        Issue issue = issueMapper.getIssueMessageById(issueId);
        if (issue == null) {
            throw new CustomException(ResultCode.PARAMETER_ERROR.getMsg(), ResultCode.PARAMETER_ERROR.getCode());
        }
        /**
         * 单选，判断
         */
        if (issue.getType() == IssueUtil.JUDGE || issue.getType() == IssueUtil.SINGLE_CHOICE) {
            if (issue.getAnswer().equals(answer)) {
                return true;
            } else {
                return false;
            }
        }
        /**
         * 多选
         */
        else if (issue.getType() == IssueUtil.MULTIPLE_CHOICE) {
            String[] issueAnswer = issue.getAnswer().split("\\$");
            String[] myAnswer = answer.split("\\$");
            List<String> rightAnswer = Arrays.asList(issueAnswer);
            List<String> reply = Arrays.asList(myAnswer);
            Boolean judge = true;
            if (reply.size() != rightAnswer.size()) {
                return false;
            }
            for (String s : reply) {
                if (!rightAnswer.contains(s)) {
                    judge = false;
                    break;
                }
            }
            if (judge) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    @Override
    public void addIssueRecord(int issueId, String answer,boolean isRight) {
        int id = ERROR;
        try {
            id = issueRecordMapper.getIdByMsg(issueId, MyHandlerInterceptor.getUid());
        } catch (Exception e) {
            id = ERROR;
        }

        Issue issue = issueMapper.getIssueMessageById(issueId);
        int iId = ERROR;
        if (id == ERROR) {
            IssueRecord issueRecord = new IssueRecord();
            issueRecord.setIssueId(issueId);
            issueRecord.setAnswer(answer);
            issueRecord.setStudentId(MyHandlerInterceptor.getUid());
            issueRecord.setCourseId(issue.getCourseId());
            if (isRight) {
                issueRecord.setIsRight(1);
                issueRecord.setRightNum(1);
                issueRecordMapper.insert(issueRecord);

            } else {
                issueRecord.setIsRight(0);
                issueRecord.setWrongNum(1);
                issueRecordMapper.insert(issueRecord);
                iId = issueRecord.getId();
            }
        } else {
            if (isRight) {
                issueRecordMapper.addRightNum(id, MyHandlerInterceptor.getUid(), answer);
            } else {
                issueRecordMapper.addWrongNum(id, MyHandlerInterceptor.getUid(), answer);
            }
        }
    }

    @Override
    public Response studentRadar(int courseId) {
        List<IssueRecord> issueRecords = issueRecordMapper.getStudentRadarMap(MyHandlerInterceptor.getUid(),courseId);
        Map<String,Double> result = new HashMap<>();
        Map<String, Integer> count = new HashMap<>();
        for(int i = 0; i < AutoUpdate.issueType.length; i++){
            result.put(AutoUpdate.issueType[i],0.0);
            count.put(AutoUpdate.issueType[i],0);
        }
        if(issueRecords == null||issueRecords.size()==0){
            return Response.ok(result);
        }
        for(IssueRecord issueRecord : issueRecords){
            Issue issue = issueMapper.getIssueMessageById(issueRecord.getIssueId());
            result.put(issue.getIssueType(),result.get(issue.getIssueType()) + (issueRecord.getRightNum()) / (issueRecord.getRightNum()+issueRecord.getWrongNum()));
            count.put(issue.getIssueType(),count.get(issue.getIssueType())+1);
        }

        for(int i = 0; i < AutoUpdate.issueType.length; i++){
            if(count.get(AutoUpdate.issueType[i])!=0) {
                result.put(AutoUpdate.issueType[i],result.get(AutoUpdate.issueType[i])/count.get(AutoUpdate.issueType[i]));
            }
        }
        return Response.ok(result);
    }

    @Override
    public Map<String, Double> getStudentRadar(int courseId) {
        List<IssueRecord> issueRecords = issueRecordMapper.getStudentRadarMap(MyHandlerInterceptor.getUid(),courseId);
        Map<String,Double> result = new HashMap<>();
        Map<String, Integer> count = new HashMap<>();
        for(int i = 0; i < AutoUpdate.issueType.length; i++){
            result.put(AutoUpdate.issueType[i],0.0);
            count.put(AutoUpdate.issueType[i],0);
        }
        if(issueRecords == null||issueRecords.size()==0){
            return result;
        }
        for(IssueRecord issueRecord : issueRecords){
            Issue issue = issueMapper.getIssueMessageById(issueRecord.getIssueId());
            result.put(issue.getIssueType(),result.get(issue.getIssueType()) + (issueRecord.getRightNum()) / (issueRecord.getRightNum()+issueRecord.getWrongNum()));
            count.put(issue.getIssueType(),count.get(issue.getIssueType())+1);
        }

        for(int i = 0; i < AutoUpdate.issueType.length; i++){
            if(count.get(AutoUpdate.issueType[i])!=0) {
                result.put(AutoUpdate.issueType[i],result.get(AutoUpdate.issueType[i])/count.get(AutoUpdate.issueType[i]));
            }
        }
        return result;
    }

    @Override
    public Map<String, Double> getStudentRadar(int courseId, int studentId) {
        List<IssueRecord> issueRecords = issueRecordMapper.getStudentRadarMap(studentId,courseId);
        Map<String,Double> result = new HashMap<>();
        Map<String, Integer> count = new HashMap<>();
        for(int i = 0; i < AutoUpdate.issueType.length; i++){
            result.put(AutoUpdate.issueType[i], 0.0);
            count.put(AutoUpdate.issueType[i],0);
        }
        if(issueRecords == null||issueRecords.size()==0){
            return result;
        }
        for(IssueRecord issueRecord : issueRecords){
            Issue issue = issueMapper.getIssueMessageById(issueRecord.getIssueId());
            result.put(issue.getIssueType(),result.get(issue.getIssueType()) + (issueRecord.getRightNum()) / (issueRecord.getRightNum()+issueRecord.getWrongNum()));
            count.put(issue.getIssueType(),count.get(issue.getIssueType())+1);
        }

        for(int i = 0; i < AutoUpdate.issueType.length; i++){
            if(count.get(AutoUpdate.issueType[i])!=0) {
                result.put(AutoUpdate.issueType[i],result.get(AutoUpdate.issueType[i])/count.get(AutoUpdate.issueType[i]));
            }
        }
        return result;
    }
}




